﻿using Dapper;
using System;
using BCEntity.Trading.SaleContractChecklist;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Trading.SaleContractChecklist
{
    /// <summary>
    /// 销售合同清单
    /// </summary>
    public class SaleContractChecklistData : ISaleContractChecklistData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM salecontractchecklist";
        private const string QUERY = @"SELECT SaleContractChecklistId,SKU,SaleGoodsNormId,NormName,NormNamePinyin,NormNo,MainImage,SaleAmount,SaleUnit,SaleCount,DeliveryTime,SaleContractId,
                                            DeliveryType,DeliveryNo,SignTime,SignMode,SaleContractNo,SaleGoodsId,SaleGoodsCategoryId,CategoryName,CategoryNamePinyin,GoodsName,GoodsNamePinyin
                                        FROM salecontractchecklist";
        public SaleContractChecklistData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查销售合同清单是否存在
        public async Task<bool> Exists(long saleContractChecklistId)
        {
            string sql = string.Format(@"{0} WHERE SaleContractChecklistId=?SaleContractChecklistId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleContractChecklistId", saleContractChecklistId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        public async Task<bool> Exists(long saleContractId,long saleGoodsId)
        {
            string sql = string.Format(@"{0} WHERE SaleContractId=?SaleContractId AND SaleGoodsId=?SaleGoodsId LIMIT 1", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleContractId", saleContractId, DbType.Int64);
            parameters.Add("?SaleGoodsId", saleGoodsId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加销售合同清单
        public async Task<SaleContractChecklistEntity> Add(SaleContractChecklistEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("salecontractchecklist", new string[] { "SKU", "SaleGoodsNormId", "NormName", "NormNamePinyin", "NormNo", "MainImage", "SaleAmount",
                "SaleUnit", "SaleCount", "SaleContractId", "SaleContractNo", "SaleGoodsId", "SaleGoodsCategoryId", "CategoryName", "CategoryNamePinyin", "GoodsName", "GoodsNamePinyin" });
            sql = string.Format("{0} {1} WHERE SaleContractChecklistId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SKU", entity.Sku?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?SaleGoodsNormId", entity.SaleGoodsNormId, DbType.Int64, size: 20);
            parameters.Add("?NormName", entity.NormName?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?NormNamePinyin", entity.NormNamePinyin?.Trim(), DbType.AnsiString, size: 800);
            parameters.Add("?NormNo", entity.NormNo?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?MainImage", entity.MainImage?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?SaleAmount", entity.SaleAmount, DbType.Decimal, size: 16);
            parameters.Add("?SaleUnit", entity.SaleUnit?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?SaleCount", entity.SaleCount, DbType.Int32, size: 11);
            parameters.Add("?SaleContractId", entity.SaleContractId, DbType.Int64, size: 20);
            parameters.Add("?SaleContractNo", entity.SaleContractNo?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?SaleGoodsId", entity.SaleGoodsId, DbType.Int64, size: 20);
            parameters.Add("?SaleGoodsCategoryId", entity.SaleGoodsCategoryId, DbType.Int64, size: 20);
            parameters.Add("?CategoryName", entity.CategoryName?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?CategoryNamePinyin", entity.CategoryNamePinyin?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?GoodsName", entity.GoodsName?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?GoodsNamePinyin", entity.GoodsNamePinyin?.Trim(), DbType.AnsiString, size: 400);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<SaleContractChecklistEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改销售合同清单
        public async Task<SaleContractChecklistEntity> Update(SaleContractChecklistEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("salecontractchecklist", new string[] { "SKU", "SaleGoodsNormId", "NormName", "NormNamePinyin", "NormNo", "MainImage", "SaleAmount",
                "SaleUnit", "SaleCount", "DeliveryTime", "SaleContractId", "DeliveryType", "DeliveryNo", "SignTime", "SignMode", "SaleContractNo", "SaleGoodsId", "SaleGoodsCategoryId",
                "CategoryName", "CategoryNamePinyin", "GoodsName", "GoodsNamePinyin" }, new string[] { "SaleContractChecklistId" });
            sql = string.Format("{0} {1} WHERE SaleContractChecklistId=?SaleContractChecklistId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleContractChecklistId", entity.SaleContractChecklistId, DbType.Int64, size: 20);
            parameters.Add("?SKU", entity.Sku?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?SaleGoodsNormId", entity.SaleGoodsNormId, DbType.Int64, size: 20);
            parameters.Add("?NormName", entity.NormName?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?NormNamePinyin", entity.NormNamePinyin?.Trim(), DbType.AnsiString, size: 800);
            parameters.Add("?NormNo", entity.NormNo?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?MainImage", entity.MainImage?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?SaleAmount", entity.SaleAmount, DbType.Decimal, size: 16);
            parameters.Add("?SaleUnit", entity.SaleUnit?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?SaleCount", entity.SaleCount, DbType.Int32, size: 11);
            parameters.Add("?DeliveryTime", entity.DeliveryTime, DbType.DateTime);
            parameters.Add("?SaleContractId", entity.SaleContractId, DbType.Int64, size: 20);
            parameters.Add("?DeliveryType", entity.DeliveryType?.Trim(), DbType.AnsiString, size: 30);
            parameters.Add("?DeliveryNo", entity.DeliveryNo?.Trim(), DbType.AnsiString, size: 30);
            parameters.Add("?SignTime", entity.SignTime, DbType.DateTime);
            parameters.Add("?SignMode", entity.SignMode?.Trim(), DbType.AnsiString, size: 30);
            parameters.Add("?SaleContractNo", entity.SaleContractNo?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?SaleGoodsId", entity.SaleGoodsId, DbType.Int64, size: 20);
            parameters.Add("?SaleGoodsCategoryId", entity.SaleGoodsCategoryId, DbType.Int64, size: 20);
            parameters.Add("?CategoryName", entity.CategoryName?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?CategoryNamePinyin", entity.CategoryNamePinyin?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?GoodsName", entity.GoodsName?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?GoodsNamePinyin", entity.GoodsNamePinyin?.Trim(), DbType.AnsiString, size: 400);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<SaleContractChecklistEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除销售合同清单
        public async Task<bool> Delete(long saleContractChecklistId)
        {
            string sql = @"DELETE FROM salecontractchecklist WHERE SaleContractChecklistId=?SaleContractChecklistId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleContractChecklistId", saleContractChecklistId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取销售合同清单
        public async Task<SaleContractChecklistEntity> GetEntity(long saleContractChecklistId)
        {
            string sql = string.Format(@"{0} WHERE SaleContractChecklistId=?SaleContractChecklistId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleContractChecklistId", saleContractChecklistId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<SaleContractChecklistEntity>(sql, parameters);
        }
        #endregion

        #region 获取销售合同清单列表
        public async Task<IEnumerable<SaleContractChecklistEntity>> GetList(long saleContractId)
        {
            string sql = string.Format(@"{0} WHERE SaleContractId=?SaleContractId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleContractId", saleContractId, DbType.Int64);
            return await this.databaseContext.Slave.QueryAsync<SaleContractChecklistEntity>(sql, parameters);
        }
        #endregion

        #region 获取销售合同清单分页
        public async Task<(IEnumerable<SaleContractChecklistEntity>, int)> Query(long? saleContractId, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (saleContractId.HasValue)
            {
                wheres.Add("SaleContractId=?SaleContractId");
                parameters.Add("?SaleContractId", saleContractId.Value, DbType.Int64);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime >=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM salecontractchecklist {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "SaleContractChecklistId" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<SaleContractChecklistEntity, int>(sql, parameters);
        }
        #endregion
    }
}



